{include file="public/head"/}
<style>
    body {
        background-color: #ffffff;
    }
</style>

<body>
    <div class="layui-form layuimini-form">

        <div class="layui-form-item">
            <label class="layui-form-label required">文章标题</label>
            <div class="layui-input-inline">
                <select name="pid" lay-verify="required" lay-reqtext="请选择文章所属分类">
                  <option value="">请选择文章所属分类</option>
                  {volist name="category" id="vo"}
                  <option value="{$vo.id}">{$vo.name}</option>
                  {/volist}
                </select>
            </div>
          </div>
  
          <div class="layui-form-item">
            <label class="layui-form-label required">文章标题</label>
            <div class="layui-input-inline">
              <input type="text" name="title" lay-verify="required" lay-reqtext="文章标题不能为空" placeholder="请输入文章名称" value="" class="layui-input" autocomplete="off">
            </div>
          </div>
  
          <div class="layui-form-item">
            <label class="layui-form-label">文章副标题</label>
            <div class="layui-input-inline">
              <input type="text" name="desc"  placeholder="请输入文章副标题" value="" class="layui-input" autocomplete="off">
            </div>
          </div>
  
          <div class="layui-form-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
              <div id="content" style="margin: 50px 0 50px 0"></div>
            </div>
          </div>
  
          <div class="layui-form-item">
            <label class="layui-form-label">文章状态</label>
            <div class="layui-input-inline">
                  <input type="checkbox" name="status" value="1" lay-skin="switch" lay-text="显示|隐藏" checked>
            </div>
          </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                <a href="javascript:;"  class="layui-btn layui-btn-primary layui-border" lay-active="closePage">关闭</a>
            </div>
        </div>
    </div>
    </div>
    <script>
        layui.use(['form','util','wangEditor'], function () {
            var form = layui.form,
                layer = layui.layer,
                util = layui.util,
                wangEditor = layui.wangEditor,
                $ = layui.$;

            //创建一个编辑器
            var editor = new wangEditor('#content');
            editor.customConfig.uploadImgServer = "{:url('UpFiles/editUpload')}";
            editor.customConfig.uploadFileName = 'file';
            editor.customConfig.pasteFilterStyle = false;
            editor.customConfig.uploadImgMaxLength = 5;
            editor.customConfig.uploadImgHooks = {
                // 上传超时
                timeout: function (xhr, editor) {
                    layer.msg('上传超时！')
                },
                // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式，可使用该配置
                customInsert: function (insertImg, result, editor) {
                    if (result.code == 0) {
                        var url = result.data.src;
                        insertImg(url);
                    } else {
                        layer.msg(result.msg);
                    }
                }
            };
            editor.customConfig.customAlert = function (info) {
                layer.msg(info);
            };
            editor.create();
            // end

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                data.field.content = editor.txt.html();
                if(data.field.status == undefined) {
                    data.field.status = 0;
                }
                // 提交到方法 默认为本身
                $.post("{:url('add')}",data.field,function(res){
                    if(res.code > 0){
                      layer.msg(res.msg,{time:1800,icon:1});
                      // 关闭弹出层
                      setTimeout(() => {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                        parent.layer.close(iframeIndex);
                      }, 1800);
                    }else{
                        layer.msg(res.msg,{time:1800,icon:2});
                    }
                });
            });
            
            util.event('lay-active',{
                closePage: function() {
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
                }
            });

        });
    </script>
    {include file="public/footer"/}